from enum import Enum
import logging

from omni_bot_sdk1.rpa.image_processor import ImageProcessor
from omni_bot_sdk1.rpa.ocr_processor import OCRProcessor
from omni_bot_sdk1.utils.size_config import suggest_size


class MenuTypeEnum(Enum):
    """
    菜单类型
    """

    Chat = "聊天"
    Contact = "联系人"
    Favorite = "收藏"
    Friend = "朋友圈"

    FriendNotification = "朋友圈通知"
    FriendSend = "朋友圈发送"
    FrendRefresh = "朋友圈刷新"

class WindowTypeEnum(Enum):
    """
    对使用到的窗口进行归类，方便查找，只使用title不可靠
    似乎微信4.x 的类名都是 Qt51514QWindowIcon，切记不可直接用类名，万一更新Qt版本就完蛋拉~
    """

    MainWindow = "MainWindow"  # <Win32Window left="0", top="0", width="1008", height="1360", title="微信">
    InviteMemberWindow = "InviteMemberWindow"  # <Win32Window left="34", top="310", width="938", height="738", title="微信添加群成员">
    InviteConfirmWindow = "InviteConfirmWindow"  # <Win32Window left="298", top="536", width="413", height="288", title="Weixin">
    InviteResonWindow = "InviteResonWindow"  # <Win32Window left="298", top="505", width="413", height="350", title="Weixin">
    RemoveMemberWindow = "RemoveMemberWindow"  # <Win32Window left="34", top="310", width="938", height="738", title="微信移出群成员">
    AddFriendWindow = "AddFriendWindow"  # <Win32Window left="274", top="312", width="450", height="825", title="通过朋友验证">
    UnableInviteWindow = "UnableInviteWindow"  # <Win32Window left="298", top="559", width="413", height="243", title="Weixin">
    SearchHistoryWindow = "SearchHistoryWindow"  # <Win32Window left="214", top="372", width="913", height="800", title="搜索聊天记录">
    FriendWindow = "FriendWindow"  # <Win32Window left="1015", top="1", width="688", height="1358", title="朋友圈">
    PublicAnnouncementWindow = "PublicAnnouncementWindow"  # <Win32Window left="158", top="278", width="709", height="812", title="“微信 GUI RPA 开发群”的群公告">
    RoomInputConfirmBox = "RoomInputConfirmBox"  # 窗口类名: Qt51514QWindowIcon <Win32Window left="298", top="556", width="413", height="246", title="Weixin">
    MenuWindow = "MenuWindow"  # <Win32Window left="0", top="0", width="1008", height="1360", title="Weixin">
    # ToolSaveBitsWindow = "ToolSaveBitsWindow"  # 搜索联系人也是这个 <Win32Window left="61", top="61", width="460", height="128", title="Weixin">
    SearchContactWindow = "SearchContactWindow"  # <Win32Window left="61", top="61", width="460", height="128", title="Weixin">


class WindowManager:
    """窗口管理器，处理所有窗口相关的操作"""
    def __init__(self,
                 image_processor: ImageProcessor,
                 ocr_processor: OCRProcessor,
                 rpa_config: dict = None,):
        self.logger = logging.getLogger(__name__)
        self.size_config = suggest_size()
        self.weixin_windows = {}
        self.current_window = None
        self.ROOM_SIDE_BAR_WIDTH = 0
        self.MSG_TOP_X = 0
        self.MSG_TOP_Y = 0
        self.MSG_WIDTH = 0
        self.MSG_HEIGHT = 0
        self.SIDE_BAR_WIDTH = 0
        self.SESSION_LIST_WIDTH = 0
        self.TITLE_BAR_HEIGHT = 0
        self.ICON_CONFIGS = {
            "send_button": {
                "name": "发送按钮",
                "color": "red",
                "position": None,
            },
            "search_icon": {
                "name": "搜索输入框",
                "color": "yellow",
                "position": None,
            },
        }
        self.image_processor = image_processor
        self.ocr_processor = ocr_processor
        self.last_switch_session = None
        self.last_switch_session_time = None
        self.rpa_config = rpa_config or {}
        # 直接将常用配置项赋值为实例属性，便于后续直接访问
        self.action_delay = self.rpa_config.get("action_delay", 0.3)
        self.side_bar_delay = self.rpa_config.get("side_bar_delay", 3)
        self.scroll_delay = self.rpa_config.get("scroll_delay", 1)
        self.switch_contact_delay = self.rpa_config.get("switch_contact_delay", 0.3)
        self.window_show_delay = self.rpa_config.get("window_show_delay", 1.5)
        self.window_margin = self.rpa_config.get("window_margin", 20)
        self.room_action_offset = tuple(
            self.rpa_config.get("room_action_offset", (0, -30))
        )
        self.search_contact_offset = tuple(
            self.rpa_config.get("search_contact_offset", (0, 40))
        )
        self.color_ranges = self.rpa_config.get("color_ranges", {})


        pass